package com.zjut.an.util.email;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.zjut.an.admin.dao.AnAdmin;
import com.zjut.an.config.dao.AnConfig;
import com.zjut.an.config.manager.AnConfigManager;
import com.zjut.an.util.system.Config;



public class SendMail extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public SendMail() {
		super();
	}
	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		//安全控制，防止未登录过的用户的非法访问
		//如果用户未登录过，而请求servlet资源，则强制转向登录界面（AdminLogin。jsp）
		HttpSession session = request.getSession(true);
		AnAdmin admin = (AnAdmin)session.getAttribute("admin");

		if (admin == null) 
		{	
			response.sendRedirect(Config.getHomePath()+"AdminLogin.jsp");
			return;
		}
		
		request.setCharacterEncoding("gb2312");
        response.setContentType("text/html;charset=gbk");
		PrintWriter out = response.getWriter();
		
		String files= request.getParameter("files");// 
		String emailaddress= request.getParameter("emailaddress");// 
		String emailtitle= request.getParameter("emailtitle");// 
		String emailcontent= request.getParameter("emailcontent");// 
		
        //设置邮箱并发送邮件
		AnConfigManager configinfomanager = new AnConfigManager();
		String configInfo1 = configinfomanager.getSingleConfigInfoByName("邮箱");
		String configInfo2 = configinfomanager.getSingleConfigInfoByName("邮箱密码");
		
        SendsimpleMail jm=new SendsimpleMail(configInfo1, configInfo2);
        boolean sendresult=jm.sendMailWithAtt(emailaddress, emailtitle, emailcontent, files);
		  
		if(sendresult){//
		    out.print("<script>alert('发送邮件成功!!');");
		    out.print("location.href='"+Config.getHomePath()+"AdminAccept.jsp'</script>");
		}else{//
		    out.print("<script>alert('"+emailaddress+"发送邮件失败!!');");
		    out.print("location.href='"+Config.getHomePath()+"AdminAccept.jsp'</script>");			
		}
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request,response);
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occure
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
